<?php
	require_once('simpletest/autorun.php');
	require_once('../PHP/reddit.php');
	require_once('../PHP/parser.php');
	require_once('../PHP/gmail.php');
	require_once('../PHP/PostContainer.php');
	require_once('../PHP/dummyParser.php');
	
	class TestAbstractParser extends UnitTestCase{
		function testSetURL() {
			$parser = new DummyParser();
			$parser->setUrl("other.com");
			$this->assertTrue($parser->url == "other.com");
		}
		
	}
	
	class TestRedditParser extends UnitTestCase {
		function testRedditParserScrapePost() {
			$parser = new RedditParser("shy");
			//$parser->setUrl("http://www.reddit.com/r/shy");
			$posts = $parser->scrapePosts();
			$this->assertTrue($posts[0]->rank == 1);
			$this->assertTrue($posts[1]->rank == 2);
			$this->assertTrue($posts[2]->rank == 3);
			$this->assertTrue($posts[0]->url == "http://reddit.com/r/shy/comments/18xtw4/could_i_get_some_advice/");
			$this->assertTrue($posts[1]->image == "");
			$this->assertTrue($posts[2]->content == "Confidence: Stepping Out");
		}
	}
	
	class TestGmailParser extends UnitTestCase {
		function testGmailParserScrapePost() {
			$user = "superscraperdummytester1001@gmail.com";
			$pass = "ss#1ss#1";
			$parser = new GmailParser($user, $pass);
			$posts = $parser->scrapePosts();
			$this->assertTrue($posts[0]->rank == 1);
			$this->assertTrue($posts[1]->rank == 2);
			$this->assertTrue($posts[2]->rank == 3);
			$this->assertTrue($posts[1]->image == "");
			$this->assertTrue($posts[2]->content 
				== "Date: Fri, 5 Apr 2013 11:00:05 -0700 From: Gmail Team (mail-noreply@google.com) Subject: Get Gmail on your mobile phone");
		}
	}
	
	class TestPostContainer extends UnitTestCase{
		
		
		function testPopulateAndGetPost() {
			$container = new PostContainer();
			$dummy = new DummyParser();
			$container->addParser($dummy);
			$container->populatePosts();
			$i=0;
			
			while (($post = $container->getNextPost()) != null) {
				$this->assertTrue(strpos($post->content, "I am a dummy ") !== false);
				$i++;
			}
		}
		
		function testRankingFormula() {
			$container = new PostContainer();
			$user = "superscraperdummytester1001@gmail.com";
			$pass = "ss#1ss#1";
			$gmail = new GmailParser($user, $pass);
			$dummy = new DummyParser();
			$container->addParser($gmail);
			$container->addParser($dummy);
			
			$container->populatePosts();
			$gs = 0;
			$ds = 0;
			for($i=0; $i<3; $i++) {
				$post = $container->getNextPost();
				if($post->parent == "Gmail")
					$gs++;
				else if($post->parent == "Dummy")
					$ds++;
			}
			$this->assertTrue($gs==2);
			$this->assertTrue($ds==1);
			$gs = 0;
			$ds = 0;
			for($i=0; $i<3; $i++) {
				$post = $container->getNextPost();
				if($post->parent == "Gmail")
					$gs++;
				if($post->parent == "Dummy")
					$ds++;
			}
			$this->assertTrue($ds==2);
			$this->assertTrue($gs==1);
			
		}
	}